//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》 http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
<template>
	<view class="page-wrapper">
		<view class="search_wrapper" @tap="gotoSearch">
			<view class="search_row">
				<image class="search-img" src="/static/images/search.png"></image>
				<view class="search-text">点击搜索</view>
			</view>
		</view>

		<scroll-view class="tab-row" :scroll-x="true">
			<view :class="'tab  ' + (index == current ? 'tab-selected' : '')" @tap="tabSelect" :data-id="index"
				v-for="(item, index) in categoryList" :key="item.k">
				{{ item.name }}
			</view>
		</scroll-view>

		<view class="goods-row" @tap="gotoGoodsDetail" :data-id="item.id" v-for="(item, index) in goodsList"
			:key="item.k">
			<image class="goods-img" :src="item.squarePic"></image>

			<view class="goods-desc">
				<view class="goods-name">{{ item.name }}</view>
				<view class="goods-price">￥{{ item.price / 100 }}</view>
			</view>
		</view>
	</view>
</template>

<script>
import {queryCategory,queryGoodsByCategory} from '@/common/api'
export default {
	data() {
		return {
			categoryList: [],
			goodsList: [],
			current: 0
		};
	},
	onLoad: function() {
		this.queryCategory();
	},
	onShow: function() {
		this.queryCategory();
	},
	methods: {
		tabSelect: function(e) {
			this.current = e.currentTarget.dataset.id;
			this.queryGoods();
		},

		queryCategory() {
			queryCategory().then(resp => {
				if (!resp.data.code) {
					this.categoryList = resp.data.data
					this.queryGoods();
				}
			})
		},

		queryGoods() {
			let _this = this;
			queryGoodsByCategory(this.getCurrentCategoryId()).then(resp => {
				if (!resp.data.code) {
					this.goodsList = resp.data.data
				}
			})
		},

		getCurrentCategoryId() {
			return this.categoryList[this.current].id;
		},

		gotoSearch() {
			uni.navigateTo({
				url: '/pages/search/search'
			});
		},

		gotoGoodsDetail(e) {
			let goodsId = e.currentTarget.dataset['id'];
			uni.navigateTo({
				url: '/pages/goods/goods?id=' + goodsId
			});
		}
	}
};
</script>
<style>
	page {
		background-color: #f4f4f4;
	}

	.page-wrapper {
		margin: 0;
		font-size: 28rpx;
		line-height: 1.5;
		color: #333;
		overflow-x: hidden;
		-webkit-overflow-scrolling: touch;
		-webkit-tap-highlight-color: transparent;
	}

	.search_wrapper {
		background-color: #ef3965;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.search_row {
		display: flex;
		flex-direction: row;
		background-color: white;
		border-radius: 10rem;
		margin-left: 1rem;
		margin-right: 1rem;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		align-items: center;
	}

	.search-img {
		height: 40rpx;
		width: 40rpx;
		margin-left: 20rpx;
	}

	.search-text {
		margin-left: 20rpx;
	}

	.tab-row {
		padding-top: 30rpx;
		width: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		white-space: nowrap;
		background-color: white;
	}

	.tab {
		width: 120rpx;
		margin-left: 10rpx;
		margin-right: 10rpx;
		display: inline-block;
		padding-bottom: 10rpx;
		text-align: center;
	}

	.tab-selected {
		border-bottom: 4rpx solid;
		color: #ef3965;
	}

	.split-view {
		height: 30rpx;
	}

	.goods-row {
		display: flex;
		flex-direction: row;
		background-color: #fff;
		border-radius: 10rpx;
		margin-top: 20rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		padding: 20rpx;
	}

	.goods-img {
		margin-left: 40rpx;
		width: 200rpx;
		height: 200rpx;
	}

	.goods-desc {
		display: flex;
		flex-direction: column;
		margin-left: 40rpx;
	}

	.goods-name {
		font-size: 35rpx;
		width: 400rpx;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.goods-price {
		margin-top: 20rpx;
		color: #ef3965;
	}
</style>
